package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.enums.TableName;
import com.itms.mapper.CarrierMapper;
import com.itms.mapper.TableDataSyncMapper;
import com.itms.pojo.Carrier;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
@EnableScheduling
@PropertySource("classpath:quartz.properties")
public class CarrierJob {

    @Reference
    private BasicDataService basicDataService;

    @Autowired
    private CarrierMapper carrierMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private SycService sycService;

    @Scheduled(initialDelayString = "${CarrierJob.initialDelayString}",fixedDelayString="${CarrierJob.fixedDelayString}" )
    public void deal(){

        try {
            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBCarrier.toString());
            Date itms = this.sycService.getMaxSyncDate(TableName.MDBCarrier.toString());
            if (mdb==null) return;;
            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) return;
            List<String>  carierCodeList = new ArrayList<>();
            Map map = new HashMap();
            //          按照carriercode分类
                List<Carrier> list=basicDataService.getCarrierList(itms);
                List<Carrier> templist = new ArrayList();
                String carrierCode = "";
                int size = list.size();
                for (int i = 0; i < size; i++) {
                    Carrier c = list.get(i);
                    if (carrierCode.equals(c.getCarrierCode())){
                        templist.add(c);
                    }else {
                        if (i > 0){
                            map.put(carrierCode,templist);
                            carierCodeList.add(carrierCode);
                            templist = new ArrayList();
                        }
                        templist.add(c);
                    }
                    if (i == size-1){
                        map.put(c.getCarrierCode(),templist);
                        carierCodeList.add(c.getCarrierCode());
                    }
                    carrierCode = c.getCarrierCode();
                }
                //            遍历分类后的结果
                for (int i = 0; i < carierCodeList.size(); i++) {
                    String c = carierCodeList.get(i);
                    List<Carrier> clist = (List<Carrier>) map.get(c);
                    boolean carrierExitsFlag = true;
                    com.itms.entity.Carrier carrier=carrierMapper.getCarrierByCarrierCode(c,null);
                    if (carrier == null){
                        carrierExitsFlag =false;
                        carrier = new com.itms.entity.Carrier();
                    }

                    int seq = 0;
                    //判断供应商联系人
                    for (Carrier mdbCarrier:clist) {
                        carrier.setCarrierCode(mdbCarrier.getCarrierCode());
                        carrier.setCarrierName(mdbCarrier.getCarrierName());
                        carrier.setIsValid(mdbCarrier.getIsValid());
                        if(mdbCarrier.getUserName()==null)continue;
                        if("".equals( mdbCarrier.getUserName()))continue;
                        seq ++ ;
                        switch(seq)
                        {
                            case 1:
                                carrier.setContactname1(mdbCarrier.getUserName());
                                carrier.setTel1(mdbCarrier.getTel());
                                break;
                            case 2:
                                if (mdbCarrier.getUserName().equals(carrier.getContactname1())){
                                    seq --;
                                }else {
                                    carrier.setContactname2(mdbCarrier.getUserName());
                                    carrier.setTel2(mdbCarrier.getTel());
                                }
                                break;
                            case 3:
                                if (mdbCarrier.getUserName().equals(carrier.getContactname1())||mdbCarrier.getUserName().equals(carrier.getContactname2())){
                                    seq --;
                                }else {
                                    carrier.setContactname3(mdbCarrier.getUserName());
                                    carrier.setTel3(mdbCarrier.getTel());
                                }
                                break;
                        }
                    }
                    if (carrierExitsFlag) {

                        carrier.setLastUpdateDate(new Date());
                        carrier.setLastUpdateUserId(new BigDecimal(9999));
                        this.carrierMapper.updateByPrimaryKeySelective(carrier);
                    } else {
                        carrier.setLastUpdateDate(new Date());
                        carrier.setLastUpdateUserId(new BigDecimal(9999));
                        carrier.setCreateDate(new Date());
                        carrier.setCreateUserId(new BigDecimal(9999));
                        this.carrierMapper.insertSelective(carrier);
                    }
                }

                this.sycService.save(mdb,TableName.MDBCarrier.toString());
                this.logService.saveErrorList(new ArrayList(), TableName.MDBCarrier.toString(), this.getClass().getName());
//            }
        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e,TableName.MDBCarrier.toString(),this.getClass().getName());
        }
    }


}
